#include <QHBoxLayout>
#include <QLineEdit>
#include <QToolButton>
#include <QDebug>
#include "cxicomboboxbase.h"
CxiComboBoxBase::CxiComboBoxBase(QWidget* parent) : QWidget(parent),
				m_tagWidget(nullptr), m_searchLineEdit(nullptr),m_popupButton(nullptr)
{
	init();
	initConnect();
}

CxiComboBoxBase::~CxiComboBoxBase() = default;
void CxiComboBoxBase::init()
{

	m_layout = new QHBoxLayout(this);

	m_layout->setContentsMargins(0, 0, 0, 0);
	m_layout->setSpacing(0);

	m_tagWidget = new QWidget(this);
	m_popupButton = new QToolButton(this);
	m_searchLineEdit = new QLineEdit(this);
	m_tagWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
	m_searchLineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

	m_tagLayout = new QHBoxLayout(m_tagWidget);
	m_tagLayout->setContentsMargins(2, 2, 2, 2);
	m_tagLayout->setSpacing(2);
	m_tagLayout->setSizeConstraint(QLayout::SetFixedSize);
	add2Layout();
}

void CxiComboBoxBase::initConnect()
{
	connect(m_searchLineEdit, &QLineEdit::returnPressed, [this]() {
		emit textEditFinished();
	});

	connect(m_popupButton, &QToolButton::clicked, [this]() {
		emit showPopup();
	});
}
void CxiComboBoxBase::setBtnText(const QString& text)
{
	m_popupButton->setText(text);
}
void CxiComboBoxBase::setLineMinWidth(int width)
{
	m_searchLineEdit->setMinimumWidth(width);
}
void CxiComboBoxBase::setBtnSize(int width, int height)
{
	m_popupButton->setFixedSize(width, height);
}
int CxiComboBoxBase::lineMinWidth() const
{
	return m_searchLineEdit->minimumWidth();
}
int CxiComboBoxBase::btnWidth() const
{
	return m_popupButton->width();
}
void CxiComboBoxBase::add2Layout(int num1, int num2, int num3)
{
	m_layout->addWidget(m_tagWidget, num1);
	m_layout->addWidget(m_searchLineEdit, num2);
	m_layout->addWidget(m_popupButton, num3);
}
void CxiComboBoxBase::setWidgetFixedWidth(int width)
{
	m_tagWidget->setFixedWidth(width);
}
void CxiComboBoxBase::refreshTagLayout()
{
	m_tagWidget->updateGeometry();
	m_tagWidget->adjustSize();
}
void CxiComboBoxBase::isShowPopupButton(int count)
{
	if (count > 200)
	{
		m_popupButton->hide();
	}
	else{
		m_popupButton->show();
	}
}
void CxiComboBoxBase::setTagWidgetLayoutPolicy()
{
	m_tagLayout->invalidate();
	m_tagWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
	m_tagWidget->setMaximumWidth(QWIDGETSIZE_MAX);
}
void CxiComboBoxBase::setUpdatesEnabled(bool enabled)
{
	m_tagWidget->setUpdatesEnabled(enabled);
}
QString CxiComboBoxBase::lineEditText() const
{
	return m_searchLineEdit->text();
}
void CxiComboBoxBase::addWidget(QWidget* widget)
{
	m_tagLayout->addWidget(widget);
}
int CxiComboBoxBase::getWidgetSpacing()
{
	return m_tagLayout->spacing();
}
void CxiComboBoxBase::resetPopupBox(QWidget* tag)
{
	removeTag(tag);
	m_tagLayout->addWidget(tag);
}
void CxiComboBoxBase::removeTag(QWidget* tag)
{
	m_tagLayout->removeWidget(tag);
}
void CxiComboBoxBase::setFocus2LineEdit()
{
	m_searchLineEdit->setFocus();
	m_searchLineEdit->setCursorPosition(m_searchLineEdit->text().length());
}
bool CxiComboBoxBase::lineEditorHasFocus()
{
	return m_searchLineEdit->hasFocus();
}
